package graphicUserInterface.mainInterface.screens;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.media.CannotRealizeException;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.protocol.DataSource;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import engine.exceptions.NoWebcamFoundException;
import engine.videoCapture.ConfigureWebcam;

public class CalibrationScreen extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1564762341964373440L;
	private static final String fstScreenID = "first Calib Part";
	private static final String sndScreenID = "second Calib Part";
	
	/* *********************************************************
	 * InnerClasses
	 * Summary : 
	 * 		- 
	 * ********************************************************* */
	private static class ChangeScreenAction implements ActionListener{
		protected String _screenLbl;
		protected CardLayout _consernedLayout;
		protected CalibrationScreen _calibScr;
		public ChangeScreenAction(String screenLabel,
				CalibrationScreen calib) {
			_screenLbl = screenLabel;
			_calibScr = calib;
			LayoutManager cLayout = calib.getLayout();
			if (cLayout instanceof CardLayout) {
				_consernedLayout = (CardLayout)cLayout;
			}else{
				_consernedLayout = null;
				throw new IllegalArgumentException("the given " +
						"container don't use a CardLayout");
			}
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			_consernedLayout.show(_calibScr, _screenLbl);
			_calibScr.validate();
		}
		
	}
	private static class ChangeToSecondScreen extends ChangeScreenAction{
		private static boolean fstChange = true;
		public ChangeToSecondScreen(CalibrationScreen calib) {
			
			super(CalibrationScreen.sndScreenID, calib);
		}
		
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(fstChange){
				fstChange = false;
				_calibScr.drawSndScreen();
			}
			super.actionPerformed(arg0);
		}
	}
	
	/* *********************************************************
	 * End of InnerClasses
	 * ********************************************************* */

	
	
	/* *********************************************************
	 * Attributes
	 * ********************************************************* */
	private JPanel _firstScreen;
	private JPanel _sndScreen;
	private JButton _next;
	private JPanel _webCamContainer;
	
	public CalibrationScreen(){
		super();
		this.setLayout(new CardLayout());
		//we will just initialize the component without drawing them
		_firstScreen = new JPanel();
		_firstScreen.setLayout(new GridLayout(1, 2));
		_sndScreen = new JPanel();
		_sndScreen.setLayout(new GridLayout(1, 2));
		_next = new JButton();
		DataSource source = null;
		Player webcamPlayer = null;
		_webCamContainer = new JPanel();
		try {
			source = ConfigureWebcam.doConfigureWebcam();
			
			webcamPlayer = Manager.createRealizedPlayer(source);
		} catch (NoWebcamFoundException e) {
			e.printStackTrace();
		} catch (NoPlayerException e) {
			e.printStackTrace();
		} catch (CannotRealizeException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(webcamPlayer != null){
				_webCamContainer.add(webcamPlayer.getVisualComponent());
				webcamPlayer.start();
			}else{
				_webCamContainer.add(
						new JLabel("webCam don't work ... Fan !"));
			}
		}
		_webCamContainer.setSize(320, 240);
		//just for debug !!!! (JFD)
		_firstScreen.setBackground(Color.GREEN);
		_sndScreen.setBackground(Color.RED);
		_webCamContainer.setBackground(Color.YELLOW);
		//end of JFD
		this.add(_firstScreen, fstScreenID);
		this.add(_sndScreen, sndScreenID);
		
	}
	
	public void drawFstScreen(){
		_firstScreen.removeAll();
		JPanel leftPart = new JPanel();
		leftPart.setLayout(null);
		leftPart.add(_webCamContainer);
		Dimension temp = _webCamContainer.getSize();
		System.out.println("Webcam container size : "+temp.width+"*"+temp.height);
		_firstScreen.add(leftPart);
		JPanel rightPart = new JPanel();
		rightPart.setLayout(new BoxLayout(rightPart, BoxLayout.Y_AXIS));
		//just for debug 
		rightPart.setBackground(Color.PINK);
		//End of JFD
		JLabel explainationPict = new JLabel(
				new ImageIcon("./assets/images/position.png"));
		rightPart.add(explainationPict);
		JButton nextScreen = new JButton("start calibration !");
		nextScreen.addActionListener(new ChangeToSecondScreen(this));
		//TODO add the calibration start call here ...
		rightPart.add(nextScreen);
		_firstScreen.add(rightPart);
		this.validate();
	}
	public void drawSndScreen(){
		_sndScreen.removeAll();
		_sndScreen.add(_webCamContainer);
		JPanel rightPart = new JPanel();
		rightPart.setLayout(new BoxLayout(rightPart, BoxLayout.Y_AXIS));
		//just for debug 
		rightPart.setBackground(Color.DARK_GRAY);
		//End of JFD
		JLabel calibrationInformations = 
			new JLabel("informations about the calibration");
		rightPart.add(calibrationInformations);
		JPanel buttonBar = new JPanel();
		buttonBar.setLayout(new GridLayout(1, 2));
		JButton restartCalibration = new JButton("restart calibration");
		buttonBar.add(restartCalibration);
		_next.setText("start a Game ");
		_next.setEnabled(false);
		buttonBar.add(_next);
		rightPart.add(buttonBar);
		_sndScreen.add(rightPart);
		this.validate();
	}
	public void showFstScreen(){
		
	}
	public void showSndScreen(){
		
	}
}
